package com.ctg.payroll2.service.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import com.ctg.payroll2.bean.po.PbCity;
import com.ctg.payroll2.bean.po.PbProvince;
import com.ctg.payroll2.dao.PbCityDAO;
import com.ctg.payroll2.dao.PbProvinceDAO;
import com.ctg.payroll2.service.ProvinceCityService;
import com.ctg.platform.orm.ibatis.IbatisEntityDao;
import com.ctg.platform.util.Page;
import com.ctg.platform.util.Parse;
 

public class ProvinceServiceImpl implements ProvinceCityService {
	private PbProvinceDAO pbProvinceDAO;
	private IbatisEntityDao ibatisEntityDao;
	public IbatisEntityDao getIbatisEntityDao() {
		return ibatisEntityDao;
	}

	public void setIbatisEntityDao(IbatisEntityDao ibatisEntityDao) {
		this.ibatisEntityDao = ibatisEntityDao;
	}

	public PbProvinceDAO getPbProvinceDAO() {
		return pbProvinceDAO;
	}

	public void setPbProvinceDAO(PbProvinceDAO pbProvinceDAO) {
		this.pbProvinceDAO = pbProvinceDAO;
	}

	public Page getProvinceCity(Map parameters, int n, int m) {
		return pbProvinceDAO.pagedQuery(parameters, n, m);
	}

	public void insertByJson(String province) {
		
	 
		
	}

	public void updateByJson(String province) {
		
/*		{"province":{"province_id":"10711","province_name":"山西省","province_code":"34","province_pinyin":"shangxi","province_abbr_pinyin":"sx"}}
		JSONObject["province"] is not a JSONArray.*/
		try{
        JSONObject jsonObject;
        jsonObject = JSONObject.fromObject(province);
//        System.out.println(province);
	    
        PbProvince entry = null;
        JSONObject o = jsonObject.getJSONObject("province");
     
         //   o = (JSONObject)modifiedItems.next();
            entry = new PbProvince();
            entry.setProvince_id(Parse.toInteger(o.getString("province_id")));
            entry.setProvince_name(o.getString("province_name"));
            entry.setProvince_code(o.getString("province_code"));
            entry.setProvince_abbr_pinyin(o.getString("province_abbr_pinyin"));
            entry.setProvince_pinyin(o.getString("province_pinyin"));
           	entry.setIs_valid(1);
 
            if(entry.getProvince_id() == null||entry.getProvince_id().equals("")){
         
            	pbProvinceDAO.insert(entry);
            }else
            	pbProvinceDAO.update(entry);
       
		}catch(Exception e){
			 e.printStackTrace();
			
		}
	 
		
	}


	public void saveProvinceCity(PbProvince o) {
		if (o.getProvince_id() == null) {
			o.setIs_valid(1);
			pbProvinceDAO.insert(o);
		} else {
			pbProvinceDAO.update(o);
		}
	} 

	public void deleteProvinceCity(List data) throws SQLException {
 
	
			if(Integer.parseInt(ibatisEntityDao.getSqlMapClient().queryForObject(((PbProvince)data.get(0)).getProvince_id()+"")+"")>0){
				
			};
		
	
		pbProvinceDAO.delete(data);

	}

	public boolean deleteProvince(List data)throws SQLException{
		
		for(int j=0;j<data.size();j++){
			Integer I = (Integer)ibatisEntityDao.getSqlMapClient().queryForObject("PbProvince.hasSubCity", ((PbProvince)data.get(j)).getProvince_id());
			if(I.intValue()>0){
				//if(Integer.parseInt(ibatisEntityDao.getSqlMapClient().queryForObject(((PbProvince)data.get(0)).getProvince_id()+"")+"")>0){
				return false;
			};
		}
		pbProvinceDAO.delete(data);
		return true;
	}
	
	
	
	  public List query(Integer provinceId, String name)
	    {
	        HashMap parameters = new HashMap();
	        if(provinceId != null)
	            parameters.put("province_id", provinceId);
	        
	        List list = pbProvinceDAO.queryForList(".getAll", parameters);
	       
	        return list;
	    }

	public void saveProvinceCity(PbCity o) {
		// TODO Auto-generated method stub
		
	}

	public List queryProvince() {
		HashMap parameters = new HashMap();
		 
		// return pbProvinceDAO.pagedQuery(parameters, 0, 200);
        List list = pbProvinceDAO.queryForList(".getProvince", parameters);
       
        return list;
	}

	public List queryCityByProviceId(int province_id) {
		// TODO Auto-generated method stub
		return null;
	}
	  
	  

}
